/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.crm.sopm.entity;

import merlion.crm.slcm.entity.Customer;
import java.io.Serializable;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import merlion.common.entity.DocFile;
import merlion.common.entity.Staff;
import merlion.scm.lcm.entity.DeliveryOrder;

/**
 *
 * @author Hong
 */
@Entity
public class PurchaseOrder implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String poNumber;
    private Timestamp purchaseOrderDate;
    private Boolean currency;
    private Boolean paymentMode;
//    private Integer indicativeLeadTime;
    private Boolean purchaseOrderStatus;
    private String comments;
    private Double taxRate;
    private String dateStr;
    private Double orderDiscount;
    private Double specialDiscount;
    private Double memberDiscount;
    private Double subtotal;
    private Double tax;
    private Double total;
    private Boolean freightOnBoard;
    private Boolean fullShipment;
    @OneToMany(targetEntity=ItemLine.class, cascade={CascadeType.PERSIST,CascadeType.MERGE}, fetch=FetchType.EAGER)
    private Collection<ItemLine> itemLines;
    @OneToOne(targetEntity = DocFile.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private DocFile uploadedImage;
    @OneToMany(targetEntity = DeliveryOrder.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<DeliveryOrder> deliveryOrders;
    @ManyToOne(targetEntity=SalesQuotation.class, cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    private SalesQuotation salesQuotation;
    @OneToOne(targetEntity = SalesOrder.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private SalesOrder salesOrder;
    @OneToOne(targetEntity = Customer.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    private Customer customer;
    @ManyToOne(targetEntity = Staff.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Staff customerExecutive;
//    @OneToOne(targetEntity = MemberDiscountGroup.class, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
//    private MemberDiscountGroup memberDiscountGroup;
    private static final SimpleDateFormat formatter = new SimpleDateFormat("MMMMM dd, yyyy");
    //To do:
    //private SalesQuotation salesQuotation; 
    
    public PurchaseOrder() {
//        for (int i = 0; i < 4; i++){
//            ItemLine newItem = new ItemLine();
//            newItem.setProduct(new Product(i));
//            orderItems.add(newItem);
//        }
        freightOnBoard = true;
        fullShipment = true;
        currency = true;
        deliveryOrders = new ArrayList<DeliveryOrder>();
        purchaseOrderDate = new Timestamp(new Date().getTime());
        dateStr = convertToDateFormat(purchaseOrderDate);
        customer = new Customer();
        salesQuotation = new SalesQuotation();
        poNumber = "";
        total = 0.0;
        taxRate = 0.07;
        memberDiscount = 0.0;
        specialDiscount = 0.0;
        orderDiscount = 0.0;
        comments="";
    }

    private String convertToDateFormat(Timestamp timeStamp) {
        return formatter.format((java.util.Date) timeStamp);
    }

    public void create(String poNumber, Customer customer, String shipTo, Double taxRate, Boolean currency) {
        this.setPoNumber(poNumber);
        this.setCustomer(customer);
        this.setCurrency(currency);
        this.setTaxRate(taxRate);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getPoNumber() {
        return poNumber;
    }

    public void setPoNumber(String poNumber) {
        this.poNumber = poNumber;
    }

    public SalesOrder getSalesOrder() {
        return salesOrder;
    }

    public void setSalesOrder(SalesOrder salesOrder) {
        this.salesOrder = salesOrder;
    }

    public Staff getCustomerExecutive() {
        return customerExecutive;
    }

    public void setCustomerExecutive(Staff customerExecutive) {
        this.customerExecutive = customerExecutive;
    }

    public String getDateStr() {
        return dateStr;
    }

    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public Double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(Double subtotal) {
        this.subtotal = subtotal;
    }

    public Double getTax() {
        return tax;
    }

    public void setTax(Double tax) {
        this.tax = tax;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Boolean getPaymentMode() {
        return paymentMode;
    }

    public void setPaymentMode(Boolean paymentMode) {
        this.paymentMode = paymentMode;
    }

    public static SimpleDateFormat getFormatter() {
        return formatter;
    }

    public DocFile getUploadedImage() {
        return uploadedImage;
    }

    public void setUploadedImage(DocFile uploadedImage) {
        this.uploadedImage = uploadedImage;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof PurchaseOrder)) {
            return false;
        }
        PurchaseOrder other = (PurchaseOrder) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.crm.sopm.entity.PurchaseOrder[ id=" + id + " ]";
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

//    public SalesQuotation getSalesQuotation() {
//        return salesQuotation;
//    }
//    public void setSalesQuotation(SalesQuotation salesQuotation) {
//        this.salesQuotation = salesQuotation;
//    }
    public Timestamp getPurchaseOrderDate() {
        return purchaseOrderDate;
    }

    public void setPurchaseOrderDate(Timestamp purchaseOrderDate) {
        this.purchaseOrderDate = purchaseOrderDate;
    }

    public Boolean getFreightOnBoard() {
        return freightOnBoard;
    }

    public void setFreightOnBoard(Boolean freightOnBoard) {
        this.freightOnBoard = freightOnBoard;
    }

    public Boolean getFullShipment() {
        return fullShipment;
    }

    public void setFullShipment(Boolean fullShipment) {
        this.fullShipment = fullShipment;
    }

    public Collection<DeliveryOrder> getDeliveryOrders() {
        return deliveryOrders;
    }

    public void setDeliveryOrders(Collection<DeliveryOrder> deliveryOrders) {
        this.deliveryOrders = deliveryOrders;
    }

    public Boolean getCurrency() {
        return currency;
    }

    public void setCurrency(Boolean currency) {
        this.currency = currency;
    }

//    public Integer getIndicativeLeadTime() {
//        return indicativeLeadTime;
//    }
//
//    public void setIndicativeLeadTime(Integer indicativeLeadTime) {
//        this.indicativeLeadTime = indicativeLeadTime;
//    }

    public Boolean getPurchaseOrderStatus() {
        return purchaseOrderStatus;
    }

    public void setPurchaseOrderStatus(Boolean purchaseOrderStatus) {
        this.purchaseOrderStatus = purchaseOrderStatus;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Double getTaxRate() {
        return taxRate;
    }

    public void setTaxRate(Double taxRate) {
        this.taxRate = taxRate;
    }

    public SalesQuotation getSalesQuotation() {
        return salesQuotation;
    }

    public void setSalesQuotation(SalesQuotation salesQuotation) {
        this.salesQuotation = salesQuotation;
    }

    public Collection<ItemLine> getItemLines() {
        return itemLines;
    }

    public void setItemLines(Collection<ItemLine> itemLines) {
        this.itemLines = itemLines;
    }

    public Double getMemberDiscount() {
        return memberDiscount;
    }

    public void setMemberDiscount(Double memberDiscount) {
        this.memberDiscount = memberDiscount;
    }

    public Double getOrderDiscount() {
        return orderDiscount;
    }

    public void setOrderDiscount(Double orderDiscount) {
        this.orderDiscount = orderDiscount;
    }

    public Double getSpecialDiscount() {
        return specialDiscount;
    }

    public void setSpecialDiscount(Double specialDiscount) {
        this.specialDiscount = specialDiscount;
    }
    
    
    //--------------------------------------------------------------------------
    //                          Sales Quotation Methods
    //--------------------------------------------------------------------------

    public void addDeliveryOrder(DeliveryOrder ol) {
        deliveryOrders.add(ol);
    }

}
